<?php
include ("../class/config.php");
include ("../class/cls_university.php");
include ("../class/cls_category.php");

$allow_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG');

if (isset($_POST['add'])) {
	$university_name = $_POST['university_name'];
	$university_price = $_POST['university_price'];
	$university_details = $_POST['university_details'];
	$university_status=$_POST['status'];
	$date = date("Y-m-d H:i:s");
	$university_image = $_FILES['university_image']['name'];
	$ext_array = explode(".", $university_image);
	$ext = array_pop($ext_array);
	
	$cat_id = $_POST['category_id'];

	$resCategory = $objCategory -> getCategoryById($cat_id);// USED FOR Category Name
	$recCategory = mysqli_fetch_array($resCategory);

	$cat_name = $recCategory['cat_name'];
	if (in_array($ext, $allow_ext)) 
	{
		move_uploaded_file($_FILES['university_image']['tmp_name'], '../uploads/university/' . $_FILES['university_image']['name']);
		$objUniversity -> addUniversity($university_name, $university_price, $university_details, $university_image, $university_status, $cat_id, $cat_name,$date);
		$success = " <span class='successmsg'>Process completed.</span>";
	} else {
		$success = "<span class='errormsg'>Process Failed !! Check the informaton below</span>";
	}
}
if (isset($_GET['deleteId'])) {
	$universityId = $_GET['deleteId'];
	$objUniversity -> deleteUniversity($universityId);
}
if (isset($_GET['editId'])) {
	$universityId = $_GET['editId'];
	if (isset($_POST['edit'])) {
		$university_name = $_POST['university_name'];
		$university_price = $_POST['university_price'];
		$university_details = $_POST['university_details'];
		$university_status=$_POST['status'];
		$date = date("Y-m-d H:i:s");
		$university_image = $_FILES['university_image']['name'];
		$ext_array = explode(".", $university_image);
		$ext = array_pop($ext_array);
	
		$cat_id = $_POST['category_id'];

		$resCategory = $objCategory -> getCategoryById($cat_id);
		$recCategory = mysqli_fetch_array($resCategory);

		$cat_name = $recCategory['cat_name'];
	if (in_array($ext, $allow_ext)) 
	{
		move_uploaded_file($_FILES['university_image']['tmp_name'], '../uploads/university/' . $_FILES['university_image']['name']);
		$objUniversity -> editUniversity($universityId, $university_name, $university_price, $university_details, $university_image,$university_status, $cat_id, $cat_name, $date);
		$success = " <span class='successmsg'>Process completed.</span>";
	} else {
		$success = "<span class='errormsg'>Process Failed !! Check the informaton below</span>";
	}

	}
}
if(isset($_GET['status']))
{
	$status=$_GET['status'];
	$uniId=$_GET['universityId'];
	$objUniversity->universityStatus($status,$uniId);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>University</title>
<script type="text/javascript" src="../lib/frmValidation.js"></script>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>

	<div class="mainWrapper">
     <?php
	include ("../includes/adminHeader.php");
	?>
    
    
    <div class="bodyContent">
    <h1>University</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>University Listing</strong>  | <a href="?action=add">Add University</a>
     
	
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th>Sn</th>
          <th>University Name</th>
          <th>Price</th>
          <th>Details</th>
          <th>Country</th>
          <th>Image</th>
          <th>Status</th>
          <th>Action</th>
        </tr>
		<?php $resUniversity= $objUniversity->selectUniversity();
		$i=1;
		
		while($recUniversity=mysqli_fetch_array($resUniversity))
		{
		?>
        <tr>
          <td><?php echo $i++; ?></td>
          <td><?php echo $recUniversity['university_name']; ?></td>
          <td><?php echo $recUniversity['university_price']; ?></td>
          <td><?php echo $recUniversity['university_des']; ?></td>
		  <td><?php echo $recUniversity['university_country']; ?></td>
		  <td><img src="../uploads/university/<?php echo $recUniversity['university_image'];?>" width=50 height=50 /></td>
		  <td>
		  	<?php 
          	if($recUniversity['university_status']=='1')
			{
          ?>
		  	<a href="?status=0&universityId=<?php echo $recUniversity['university_id']?>"> 
		  	<img src="../images/active.jpg" alt="Active"/></a>
		  <?php } else {
		  	?>
		  	<a href="?status=1&universityId=<?php echo $recUniversity['university_id']?>"> 
		  	<img src="../images/inactive.jpg" alt="Inactive"/></a>
		  	<?php }?>
		  </td>
          <td><a href="?action=edit&editId=<?php echo $recUniversity['university_id']; ?>">Edit</a> | <a href="?deleteId=<?php echo $recUniversity['university_id']; ?>" onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a></td>
        </tr>
        <?php } /// end of while($recCategory=mysqli_fetch_array($resCategory)) ?>
       </table>
       
     <?php }
		else if(isset($_GET['action']) && $_GET['action']=='edit')
		{
		$universityId=$_GET['editId'];
		$res=$objUniversity->getUniversityById($universityId);
		$rec=mysqli_fetch_array($res);
	 ?>
      <strong>Edit University</strong>
      <?php
	if (isset($success)) {
		echo $success;
	}?>
      <form id="categoryEdit" name="categoryEdit" method="post" action="" enctype="multipart/form-data" 
      		onsubmit="return editCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">University Name *</td>
            <td><input type="text" name="university_name" id="university_name" class="textBox" value="
            	<?php echo $rec['university_name'];?>" />
            	<?php if(isset($_POST['university_name']) && $_POST['university_name']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?></td>
          </tr>
          <tr>
            <td width="30%">University Price *</td>
            <td><input type="text" name="university_price" id="university_price" class="textBox" value="
            	<?php echo $rec['university_price'];?>"/>
            	<?php if(isset($_POST['university_price']) && $_POST['university_price']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?></td>
          </tr>
          <tr>
          	<td> Country </td>
            <td> <select name="category_id" id="category_id">
            	 <option value="0">--SELECT--</option>
                <?php $resCategory=$objCategory->selectCategory();
				while($recCategory=mysqli_fetch_array($resCategory))
				{?>
                <option value="<?php echo $recCategory['cat_id']; ?>"><?php echo $recCategory['cat_name']; ?></option>
                <?php } ?>
                </select>
            </td>
          </tr>
            <td width="30%">University Details *</td>
            <td><textarea rows="5" cols="50" name="university_details" id="university_details" class="textBox">
            	<?php echo $rec['university_des'];?></textarea>
            	<?php if(isset($_POST['university_details']) && $_POST['university_details']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?></td>
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="university_image" id="university_image"  />
          		<?php if(isset($_FILES['university_image']['name']) &&$_FILES['university_image']['name']==""){
            		echo "<span class='errormsg'>no file chosen !</span>";
            	}
					if(isset($_FILES['university_image']['name']) &&$_FILES['university_image']['name']==""){
					$university_image=$_FILES['university_image']['name'];
				
            	$ext_array = explode(".",$university_image);
				$ext = array_pop($ext_array);
				
				if(!in_array($ext, $ext_array)){
					echo "<span class='errormsg'>invalid formate !</span>";
				}}?>?>
          	</td>
          </tr>
          <tr>
          	<td>Status : </td>
            <td><input type="radio" name="status" value="1" checked="checked"  />Active
            	<input type="radio" name="status" value="0" />Inactive</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="edit" id="edit" value="Edit" />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='university.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
 <?php }
		else if(isset($_GET['action']) && $_GET['action']=='add')
		{
	 ?>
      <strong>Add University</strong>
      <?php
	if (isset($success)) {
		echo $success;
	}
 ?>
      <form id="addUniversity" name="addUniversity" method="post" action="" enctype="multipart/form-data" onsubmit="return addCat()">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1">
          <tr>
            <td width="30%">University Name *</td>
            <td><input type="text" name="university_name" id="university_name" class="textBox" />
            	<?php if(isset($_POST['university_name']) && $_POST['university_name']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?>
            </td>
          </tr>
          <tr>
            <td width="30%">University Price *</td>
            <td><input type="text" name="university_price" id="university_price" class="textBox" />
            	<?php if(isset($_POST['university_price']) && $_POST['university_price']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?>
            </td>
          </tr>
          <tr>
          	<td> Country </td>
            <td> <select name="category_id" id="category_id">
            	 <option value="0">--SELECT--</option>
                <?php $resCategory=$objCategory->selectCategory();
				while($recCategory=mysqli_fetch_array($resCategory))
				{?>
                <option value="<?php echo $recCategory['cat_id']; ?>"><?php echo $recCategory['cat_name']; ?></option>
                <?php } ?>
                </select>
            </td>
          </tr>
          <tr>
            <td width="30%">University Details *</td>
            <td><textarea rows="5" cols="50" name="university_details" id="university_details" class="textBox" /></textarea>
            <?php if(isset($_POST['university_details']) && $_POST['university_details']==""){
            		echo "<span class='errormsg'>empty university name !</span>";
            	} ?></td>
          </tr>
          <tr>
          	<td width="30%">Image </td>
          	<td><input type="file" name="university_image" id="university_image"  />
          		<?php if(isset($_FILES['university_image']['name']) &&$_FILES['university_image']['name']==""){
            		echo "<span class='errormsg'>no file chosen !</span>";
            	}
            	if(isset($_FILES['university_image']['name']) &&!$_FILES['university_image']['name']==""){
				$university_image=$_FILES['university_image']['name'];
				
            	$ext_array = explode(".", $university_image);
				$ext = array_pop($ext_array);
				
				if(!in_array($ext, $ext_array)){
					echo "<span class='errormsg'>invalid formate !</span>";
				}}?>
          	</td>
          </tr>
          <tr>
          	<td>Status : </td>
            <td><input type="radio" name="status" value="1" checked="checked"  />Active
            	<input type="radio" name="status" value="0" />Inactive</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="add" id="add" value="Add"  />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='university.php'" /></td>
          </tr>
          <tr>
            <td>Note : * Fields are mandatory.</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form>
<?php } ?>
    </div>
    
    </div>
    
    
    <?php
	include ("../includes/adminFooter.php");
	?>
     
       </div>

</body>
</html>